<template>
	<div class="port-page">
		<!-- 顶部按钮 --> 
		<div class="top" style="width: 100%;margin: 0 auto;">
			<div class="but1">
				<router-link to="/home">
					<el-button type="warning" size="small" style='margin-right: 10px;' icon="el-icon-menu">首页</el-button>
				</router-link> 
					<el-button type="warning" size="small" style='margin-right: 10px;' icon="el-icon-caret-left" @click='cancelf'>返回</el-button>
			</div>
			
			<div style="text-align: center;">
				<el-steps :active="active" style=" width: 40%;margin-left: 30%;" finish-status="success">
					<el-step title="下载模板"></el-step>
					<el-step title="导入学生"></el-step>
					<el-step title="录入班级"></el-step>
					<el-step title="完成"></el-step>
				</el-steps> 
				<br/>
			<el-button @click="fh" v-if="fhshow">返回</el-button>
			<el-button @click="xyb" v-if="xybshow">下一步</el-button>
			<el-button @click="xyb" v-if="jxshow">继续</el-button>
		  </div> 
		</div> 
		<!--第一步-->
		<div class="dyb" v-if="next1">
			<p>下载模板</p>
			<p>填写学生信息</p>
			<p>
				<a href="http://47.93.124.148:8080/uplod/studentmodel.xlsx">
					<el-button type="danger" class="submit" icon="el-icon-download" @click="">下载模板</el-button>
				</a>
			</p>
		</div>
		<div class="deb" v-if="next2">
			<p>选择文件导入学生</p>
			<el-upload class="upload-demo" :action="wjurl" :on-success="successlist" :on-preview="handlePreview" :on-remove="handleRemove" :file-list="fileList">
				<el-button type="primary" class="submit" icon="el-icon-edit-outline">选取文件</el-button>
			</el-upload>
		</div>
		<!-- 模糊搜索条件 -->

		<div class="search" v-if="bglist">
			<p style="font-size: 20px;">请选择班级进行保存</p>
			<div class="search-item" style="margin-left: 30%;">
				<el-select v-model="vbjid" class="code-search" placeholder="请选择班级名称">
					<el-option :label="v.mc" :value="v.id" v-for="v in comboxlist"></el-option>
				</el-select>
			</div>
			<el-button type="success" size="small" style='margin-left: 10px;' v-if='bc' @click='submit'>保存</el-button>
		</div>
		<el-table v-if='bglist' :cell-style="styletest"  :data="dataList" border stripe align="center" class="admin-table" sum-text :row-class-name="tableRowClassName" @select="checkSelect" @select-all="checkall">
			<div slot='empty'>
				<img src="../../../assets/img/empty.png" style="width:40px;vertical-align: middle;">
				<span style="vertical-align: middle;">暂时还没有内容哦!</span>
			</div>
			<el-table-column :prop="v.prop" :label="v.label" v-for="(v,index) in portTitleList" align="center" show-overflow-tooltip v-if="v.hide != 0" :formatter="v.formatter" :align="v.align || 'center'" :fixed="v.fixed" :class-name="v.columnStyle" :width="v.width" :min-width="v.minWidth" :header-align="v.headerAlign || 'center'">
				<template scope="scope">
					<div slot="reference" :style="tableCellControl(scope)" v-if="scope.column.property === 'sm' ">
						<!--<el-input class="content-input" style="height: 40px;" v-model="scope.row[scope.column.property]"></el-input>-->
						<!-- :disabled="scope.row.type === '物品' && scope.column.property === 'zk' ? true:false" -->
					</div>

					<div slot="reference" v-else>
						{{ scope.row[scope.column.property] }}
					</div>
				</template>
			</el-table-column>

			<el-table-column label="操作" align="center" width="100px" fixed="right">
				<template scope="scope">
					<el-button size="small" type="danger" @click="yichu(scope)" v-for="v in portBntList">{{v.name}}</el-button>
				</template>
			</el-table-column>
		</el-table>
		<div class="two" v-if='dey' style="width: 100%;height: 100%;margin: 0 auto;">
			<div style="width: 100%;float: left;">
				<div class="sm" style="text-align: left;margin: 0 auto;">
					<div style="text-align: center;line-height: 50px;">成功导入&nbsp;<span style="color: red;">{{cgrs}}</span>&nbsp;个学生，是否继续？</div>
				</div>
			</div>
		</div>
		<LoadingComponent v-show="isLoading"></LoadingComponent>
		<!-- 子路由显示区域 -->
		<router-view class="port-view"></router-view>
	</div>
</template>
<script>
	import {
		mapState
	} from 'vuex'
	import qs from 'qs'
	import LoadingComponent from 'components/loading'
	export default {
		components: {
			LoadingComponent,
		},
		data() {
			return {
				fhshow: false,
				xybshow: true,
				jxshow: false,
				next1: true,
				next2: false,
				bglist: false,

				active: 0,
				cgrs: '',
				path: '',
				sy: true,
				dey: false,
				bc: false,
				fileList: [],
				fileList1: [],
				wjurl: this.$store.state.api + '/user/saveimg',
				//	wjurl:'http://192.168.3.150:8080/jiayin-controller/user/saveimg',
				vbjid: '',
				bjlxparams: {
					bjlx: 'Y'
				}, //
				/*模糊搜索条件*/
				visqy: 'Y',
				vkeyword: '', // keyword
				vbtime: '', // 开始时间
				vetime: '', // 结束时间
				multipleDelNum: '', //获取批量选择的数据
				dataList: [], //表格数据
				comboxlist: [], //下拉框 动态的
				vcomautovalue: '', //下拉框name 动态的
				vcomgdvalue: '', //下拉框name 固定的

				/*动画加载*/
				isLoading: true, //动画加载
				searchLoading: false, //搜索按钮触发按钮动画
				/*分页属性设置*/
				layout: 'total, sizes, prev, pager, next, jumper', //组件布局
				pageindex: this.$store.state.pageindex, //当前分页页码
				pagesizes: this.$store.state.pagesizes, //显示个数的选择器选项
				pagesize: this.$store.state.pagesize, //每页显示条目数
				total: 0, //总条数

				/*组件区域*/
				portComponents: {
					/* 单行删除  */
					singleDelete: {
						type: 'singleDelete',
						toggle: false,
						params: '',
						data: {
							index: '',
							type: 'single',
							title: '此操作将删除该记录?, 是否继续?'
						}
					},
					/* 批量删除 */
					multipleDelete: {
						type: 'multipleDelete',
						toggle: false,
						params: '',
						data: {
							index: '',
							type: 'multiple',
							title: '此操作将删除您选中的记录?, 是否继续?'
						}
					},

				},
				/*
				    prop :  接口参数名
				    label : 接口中文名
				    hide : 是否隐藏这一列
				    align : 这一列对齐顺序 ： left,right,center
				    headerAlign : 这一列表头对齐方式 ： left,right,center
				*/
				portTitleList: [

					{
						prop: 'id',
						label: '编号',
						hide: 0,
						align: 'center',
						fixed: false,
						width: '',
						minWidth: '',
						headerAlign: 'center',
						formatter: function(row, column) {}
					}, {
						prop: 'xsmc',
						label: '中文名',
						hide: 1,
						align: 'center',
						fixed: false,
						width: '',
						minWidth: '100',
						headerAlign: 'center',
						formatter: function(row, column) {}
					}, {
						prop: 'ywm',
						label: '英文名',
						hide: 1,
						align: 'center',
						fixed: false,
						width: '',
						minWidth: '100',
						headerAlign: 'center',
						formatter: function(row, column) {}
					},

					{
						prop: 'xb',
						label: '性别',
						hide: 1,
						align: 'center',
						fixed: false,
						width: '',
						minWidth: '60',
						headerAlign: 'center',
						formatter: function(row, column) {}
					}, {
						prop: 'csrq',
						label: '出生日期',
						hide: 1,
						align: 'center',
						fixed: false,
						width: '',
						minWidth: '100',
						headerAlign: 'center',
						formatter: function(row, column) {}
					},

					{
						prop: 'dh',
						label: '学生电话',
						hide: 1,
						align: 'center',
						fixed: false,
						width: '',
						minWidth: '120',
						headerAlign: 'center',
						formatter: function(row, column) {}
					}, {
						prop: 'fmdh',
						label: '父母电话',
						hide: 1,
						align: 'center',
						fixed: false,
						width: '',
						minWidth: '100',
						headerAlign: 'center',
						formatter: function(row, column) {}
					}, {
						prop: 'fmwx',
						label: '父母微信',
						hide: 1,
						align: 'center',
						fixed: false,
						width: '',
						minWidth: '',
						headerAlign: 'center',
						formatter: function(row, column) {}
					}, {
						prop: 'jtdz',
						label: '家庭地址',
						hide: 1,
						align: 'center',
						fixed: false,
						width: '',
						minWidth: '',
						headerAlign: 'center',
						formatter: function(row, column) {}
					}, {
						prop: 'njbj',
						label: '年级',
						hide: 1,
						align: 'center',
						fixed: false,
						width: '',
						minWidth: '100',
						headerAlign: 'center',
						formatter: function(row, column) {}
					}, {
						prop: 'glxx',
						label: '公立校',
						hide: 1,
						align: 'center',
						fixed: false,
						width: '',
						minWidth: '',
						headerAlign: 'center',
						formatter: function(row, column) {}
					}, {
						prop: 'note',
						label: '数据验证',
						hide: 1,
						align: 'center',
						fixed: false,
						width: '100',
						minWidth: '',
						headerAlign: 'center',
						formatter: function(row, column) {}
					}, {
						prop: 'yuer',
						label: '账户余额',
						hide: 0,
						align: 'center',
						fixed: false,
						width: '100',
						minWidth: '',
						headerAlign: 'center',
						formatter: function(row, column) {}
					}, {
						prop: 'ks',
						label: '上课课时',
						hide: 0,
						align: 'center',
						fixed: false,
						width: '',
						minWidth: '',
						headerAlign: 'center',
						formatter: function(row, column) {}
					},

				],
				portBntList: [ //接口按钮列表
					{
						name: '移除',
						type: 'singleDelete'
					}
				],
			}
		},
		
		created() {
			//加载页面时调用
			this.$store.dispatch('changeTitle', '学生导入'); //修改title
			this.$store.dispatch('changeRouterIndex', '/educa/xsdrexcelst'); //修改路由
			this.getcomlist(); //加载下拉列表
			this.isLoading = true; //动画开始
		},
		mounted() {
			//加载完成之后event
			this.isLoading = false; //动画结束
			//this.lj();
		},
		methods: {
			xyb() {
				this.active++
					if(this.active == 1) {
						this.fhshow = true
						this.xybshow = false
						this.next1 = false
						this.next2 = true
					}
				if(this.active == 2) {
					this.next1 = false
					this.next2 = false
					this.bglist = true
				}
				if(this.active == 3) {
					this.next1 = false
					this.next2 = false
					this.bglist = false
					this.xybshow = false
					this.jxshow = true
					this.dey = true;
					this.bc = false
				}
				if(this.active == 4) {
					this.active = 1
					this.jxshow = false
					this.fhshow = true
					this.xybshow = false
					this.next1 = false
					this.next2 = true
					this.dey = false;
				}
			},
			fh() {
				this.active--
					if(this.active == 0) {
						this.fhshow = false
						this.xybshow = true
						this.next1 = true
						this.next2 = false
					}
				if(this.active == 1) {
					this.next1 = false
					this.next2 = true
					this.bglist = false
					this.bc = false
				}
				if(this.active == 2) {
					this.next1 = false
					this.next2 = false
					this.bglist = true
					this.jxshow = false
					this.xybshow = true
					this.dey = false;
					this.bc = false
				}
			},
			yichu(id) {
				this.dataList.splice(id.$index, 1)
			},
			lj() { 
				var logininfo = JSON.parse(localStorage.logininfo); 
				this.dataList = [];
				var params2 = qs.stringify({
						path: this.path,
						xxid: logininfo.xxid,
						yhid: logininfo.id
					})
					//执行
				this.axios.post(this.$store.state.api + '/readexce2', params2)
					.then(res => {
						if(res.data.code == 1) {
							this.sy = false
							this.bglist = true
							this.bc = true
							var xslist = []
							xslist = res.data.data
							for(var i in xslist) {
								this.dataList.push(xslist[i][0])
							}
						} else {
							this.$message.warning(res.data.msg);
						}

					}).catch(res => {})
			},
			handleRemove(file, fileList) {
				//		        console.log(file, fileList,9494944);
			},
			handlePreview(file) {
				//		        console.log(file,88888);
			},
			successlist(res, file, fileList) {
				this.path = res
//					console.log( this.path,'9999999999999999')
				this.active = 2
				this.next1 = false
				this.next2 = false
				this.lj();
			},
			handleRemove1(file, fileList) {},
			handlePreview1(file) {},
			successlist1(res, file, fileList) {
				this.dey = false
				this.path = res
				this.lj();
			},
			getcomlist() {
				var logininfo = JSON.parse(localStorage.logininfo);
				var params2 = qs.stringify({
						yhid: logininfo.id + '',
						xxid: logininfo.xxid + ''
					})
					//执行
				this.axios.post(this.$store.state.api + 'bj/getbj', params2)
					.then(res => {
						let data = res.data.data;
						let list = data.list;
						this.comboxlist = list;
					}).catch(res => {})
			},
			cancelf() {
				this.$router.back(-1)
			},
			submit() { 
				 if (this.vbjid==''){
					 this.$message.warning("请先选择导入的班级，才能完成导入");
				 	return ;
				 }
				
				var sum = 0;
				this.dataList.forEach((v, i) => {
					var logininfo = JSON.parse(localStorage.logininfo);
					var params3 = qs.stringify({
						xsmc: v.xsmc,
						ywm: v.ywm,
						xb: v.xb,
						dh: v.dh,
						csrq: v.csrq + '',
						fmdh: v.fmdh,
						fmwx: v.fmwx,
						jtdz: v.jtdz,
						njbj: v.njbj,
						glxx: v.glxx,
						yuer: v.yuer,
						ks: v.ks,
						bjid: this.vbjid,
						yhid: logininfo.id,
						xxid: logininfo.xxid,
					});
					this.axios.post(this.$store.state.api + 'xs/insertdr', params3)
						.then(res => {
							if(res.data.code == 1) {
								this.$store.state.loading = false;
								this.$message.success('保存成功！');
								this.bglist = false;
								this.sy = false;
								this.bc = false;
								this.dey = true;
								this.active = 3
								this.jxshow = true;
							} else {
								this.$message.warning(res.data.msg);
								this.$store.state.loading = false;
							}
						}).catch(res => {
							this.$message.warning("服务器异常！");
							this.$store.state.loading = false;
						})
					sum++;
				})
				this.cgrs = sum;
			},
	styletest(boj){ 
            if(this.dataList[boj.rowIndex].ys == '1'){
              return ' color:black';
            		}else     if(this.dataList[boj.rowIndex].ys == '2'){
              return ' color:red';
            		}else    if(this.dataList[boj.rowIndex].ys == '3'){
              return ' background:yellow';
            		}
    				return '';
    	 },
			tableRowClassName(row, index) { //隔行变色颜色设置
				/*if ( index%4 == 0) {
				    return "table-cell-green";
				}else if( index%4 == 1 ){
				    return "table-cell-yellow";
				}else if( index%4 == 2 ){
				    return "table-cell-red";
				}else{
				    return "table-cell-blue"
				}*/
			},
			tableCellControl(scope) {
				//表格单元格控制CSS样式
				/*if (scope.row.id == 112 && scope.column.property == "iname") {
				    return {
				        color:"red",
				    }
				}else{
				    return {
				        color:"green",
				    }
				}*/
			},
			handleSizeChange(val) { //每页显示条数触发事件
				this.pagesize = val;
				this.pageindex = 1;
			},
			handleCurrentChange(val) { //分页触发事件
			},
			checkSelect(selection, row) { //点击单选select触发事件
				let strID = [];
				selection.forEach(function(v, i) {
					strID.push(v.id)
				})
				this.multipleDelNum = strID.join(",");
			},
			checkall(selection) { //点击全选select触发事件
				let strID = [];
				selection.forEach(function(v, i) {
					strID.push(v.id)
				})
				this.multipleDelNum = strID.join(",");
			},
			/* 组件的弹框  */
			componentsShow(type, data) { //显示组件
				for(let i in this.portComponents) {
					if(type == this.portComponents[i].type) {
						this.portComponents[i].toggle = true;
						this.portComponents[i].params = data;
						if(this.portComponents[i].data.type == 'single') {
							this.portComponents[i].data.index = data.row.id
						} else if(this.portComponents[i].data.type == 'multiple') {
							this.portComponents[i].data.index = this.multipleDelNum;
						} else {
							return;
						}
						return;
					}
				}
			},
			componentsHide(setting) { //关闭组件
				//              this.search();s
				for(let i in this.portComponents) {
					if(setting.type == this.portComponents[i].type) {
						this.portComponents[i].toggle = false;
						if(!setting.cancel) {
							//                         this.search();
						}
						return;
					}
				}
			}
		},
		computed: {
			...mapState([
				'api' //域名
			]),
			multipleDeleteDisabled() {
				if(this.multipleDelNum) {
					return false;
				} else {
					return true
				}
			}
		}
	}
</script>
<style lang="less" scoped>
	.dyb {
		width: 500px;
		height: 300px;
		margin: 0 auto;
		margin-top: 50px;
		p {
			width: 100%;
			text-align: center;
			height: 50px;
			line-height: 50px;
			font-size: 20px;
		}
	}
	
	.deb {
		width: 500px;
		height: 300px;
		margin: 0 auto;
		margin-top: 50px;
		p {
			width: 100%;
			text-align: center;
			height: 50px;
			line-height: 50px;
			font-size: 20px;
		}
	}
	
	.port-page {
		overflow: hidden;
		font-size: 12px;
		.btn {
			height: 30px;
			padding: 0 10px;
			margin-left: 0;
		}
		.code-search {
			height: 30px;
			margin: 10px 0 10px 0;
			min-width: 120px;
			max-width: 150px;
		}
		.class-page {
			text-align: left;
			margin-top: 10px;
		}
		.search-item {
			display: inline-block;
			margin-right: 10px;
		}
		.but1 {
			width: 100%;
			float: left;
		}
		.search {
			width: 60%;
			float: right;
			text-align: left;
		}
		.sm {
			width: 80%;
			height: 40%;
			margin: 0 auto;
			border: 1px solid #CCCCCC;
			text-align: center;
			background: white;
			font-size: 20px;
		}
		.sm p {
			margin-top: 10px;
			width: 50%;
			margin-left: 39%;
		}
		.el-button+.el-button {
			margin-left: 0px;
			margin-bottom: 5px;
		}
		/*.el-table--enable-row-transition .el-table__body td {
    transition: background-color .25s ease;
    width: 200px;
}*/
	}
	
	.upload-demo {
		width: 100%;
		text-align: center;
		float: left;
		margin-top: 20px;
	}
	
</style>